<template>
  <div class="user-box">
      <s-header :name="'配送至'"></s-header>
      <div class="cell">
        <van-cell title="当前配送至" icon="location-o" :value="address" />
      </div>
      <div class="address">      
        <span class="font-address">从我的地址中选择</span>
        <div v-for="item in list" :key="item.id" @click="ship(item.name,item.tel,item.province_name,item.city_name,item.region_name,item.detail_address,item.id)">
          <p v-if="item.isDefault">          
            <van-cell :title="item.name + '[默认地址]'" :value="item.tel" :label="item.address"/>
          </p>
          <p v-else>
            <van-cell :title="item.name " :value="item.tel" :label="item.address"/>
          </p>
        </div>
    </div>
  </div>
</template>

<script>
import sHeader from '@/components/SimpleHeader'

export default {
components: {
  sHeader
},
data(){
  return {
      signature:'',
      id:'',
      mobile:'',
      code:'',
      sms_code:'',
      chosenAddressId:'1',
      list:[],
      address:'',
      defaultID:'',
      user_id:'',
      ship_id:''
  }
},
created(){
  this.user_id = this.$cookies.get('id')
  this.id=this.$cookies.get('id')
  this.$axios.get(this.$settings.BASE_URL + 'address/ship/?user_id=' + this.id,    
  {headers:{'Authorization':'jwt ' + this.$cookies.get('token')}} ).then(res=>{
        if(!(this.$store.state.province_name&&this.$store.state.city_name&&this.$store.state.region_name)){     
        this.province = res.result.province_name
        this.city = res.result.city_name
        this.region = res.result.region_name
        this.address = res.result.province_name + ' ' + res.result.city_name + ' ' + res.result.region_name
      }else{
        this.province = this.$store.state.province_name
        this.city = this.$store.state.city_name
        this.region = this.$store.state.region_name
        this.address = this.$store.state.province_name + ' ' + this.$store.state.city_name + ' ' + this.$store.state.region_name
      }
    })
  this.$axios.get(this.$settings.BASE_URL + 'address/detail/?user_id=' + this.id,    
  {headers:{'Authorization':'jwt ' + this.$cookies.get('token')}}).then(res=>{
    this.list = res.result
    this.defaultID=res.result.id
  })
  this.$axios.get(this.$settings.BASE_URL + 'address/ship/?user_id=' + this.id,    
  {headers:{'Authorization':'jwt ' + this.$cookies.get('token')}}).then(res=>{
    this.ship_id = res.result.id
  }) 
},
methods:{
  ship(item_name,item_tel,item_province_name,item_city_name,item_region_name,item_detail_address,item_id){
    this.$store.dispatch('deafultAddress',
          [item_name,item_tel,item_province_name,item_city_name,item_region_name,item_detail_address,item_id])
          this.$router.go(-1)
    this.$axios.put(this.$settings.BASE_URL + 'address/update_ship/' + this.ship_id +'/?user_id=' + this.user_id, {
        ship_address : 0
    },{headers:{'Authorization':'jwt ' + this.$cookies.get('token')}}).then(res=>{})
    this.$axios.put(this.$settings.BASE_URL + 'address/update_ship/' + item_id +'/?user_id=' + this.user_id, {
        ship_address : 1
    },{headers:{'Authorization':'jwt ' + this.$cookies.get('token')}}).then(res=>{})
  }
 }
}
</script>


<style lang="less" scoped>
@import '../common/style/mixin';

.user-box {
  .cell{
    margin-top: 50px;
  }
  .address{
    margin-top: 25px;
  }
  .font-address{
    margin-left:15px;
  }
  }

</style>